package com.cskaoyan.wxcontroller;

import com.cskaoyan.bean.baseresultVO.*;
import com.cskaoyan.bean.brand.brandVo.CommentVOVO;
import com.cskaoyan.bean.domain.*;
import com.cskaoyan.bean.order.OrderId.OrderId;
import com.cskaoyan.bean.order.OrderVO;
import com.cskaoyan.bean.order.baseorders.BaseOrder.OrderShowType;
import com.cskaoyan.bean.order.wxGetOrder.OrderDetailForUser;
import com.cskaoyan.bean.order.wxGetOrder.WxOrderBO;
import com.cskaoyan.bean.wxComment.CommentVo;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.shiro.MallToken;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.lang.System;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author looper
 * @ 类目控制器
 **/

@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    OrderService service;


    @GetMapping("goods")
    public BaseRespVo getGoods(OrderGoods orderGoods){
        //orderID与GoodsID
        List<OrderGoods> orderGoodsVo =  service.getGoods(orderGoods);
        return  BaseRespVo.ok(orderGoodsVo.get(0));
    }




    @GetMapping("list")
    public BaseRespVo getList(OrderShowType OrdershowType, BaseParam baseParam, HttpSession session) {

        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        // TODO: 2021/12/2 session
        //showType表示订单状态 查询当前用户的订单信息
        BaseData baseData = service.getOrderListForUser(OrdershowType, baseParam, user);

        return BaseRespVo.ok(baseData);
    }


    @GetMapping("detail")
    public BaseRespVo getDetail(Integer orderId) {
        //因为订单id只有一个,所以不用考虑用户可见的问题
        Order order = new Order();
        order.setId(orderId);
        OrderDetailForUser orderDetailForUser = service.getOrderdetailForUser(order);
        return BaseRespVo.ok(orderDetailForUser);

    }

    @PostMapping("delete")
    public BaseRespVo getDelete(@RequestBody OrderId orderId){

        service.deleteByUpdate(orderId.getOrderId());
        return BaseRespVo.ok(null);
    }

    @PostMapping("cancel")
    public BaseRespVo getCancel(@RequestBody OrderId orderId){
        Order order = new Order();
        order.setId(orderId.getOrderId());
        //102代表用户取消
        order.setOrderStatus(Short.valueOf("102"));
        //把status改成102
        service.getChangeStatus(order);
        return BaseRespVo.ok(null);
    }

    @PostMapping("refund")
    public BaseRespVo getRefund(@RequestBody OrderId orderId){
        Order order = new Order();
        order.setId(orderId.getOrderId());
        //202表示申请退款
        order.setOrderStatus(Short.valueOf("202"));
        service.getChangeStatus(order);
        return  BaseRespVo.ok(null);
    }


    @PostMapping("confirm")
    public BaseRespVo getConfirm(@RequestBody OrderId orderId){
        Order order = new Order();
        order.setId(orderId.getOrderId());
        //202表示申请退款
        order.setOrderStatus(Short.valueOf("401"));
        service.getChangeStatus(order);
        return  BaseRespVo.ok(null);
    }




    @PostMapping("comment")
    public BaseRespVo getComment(@RequestBody CommentVOVO comment){


//        // TODO: 2021/12/2
        Integer orderGoodsId = comment.getOrderGoodsId();

        Subject subject = SecurityUtils.getSubject();

        /**
         * 从session中获取对象
         */
        Comment commentpojo = new Comment();

        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
                                
        comment.setUserId(user.getId());
        BeanUtils.copyProperties(comment,commentpojo);
        Integer code = service.getComment(commentpojo,orderGoodsId);
        return BaseRespVo.ok(null);
    }

    @PostMapping("prepay")
    public  BaseRespVo getPrepay(@RequestBody  OrderId orderId){
        Subject subject = SecurityUtils.getSubject();
        /**
         * 从session中获取对象
         */
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        if (user==null){
            return BaseRespVo.error(402,"请先登入");
        }

//        Order order = new Order();
//        order.setId(orderId.getOrderId());
//        order.setOrderStatus((short) 201);
//        order.setUserId(user.getId());
//        order.setUpdateTime(LocalDateTime.now());
//
//        service.getChangeStatus(order);

//        return BaseRespVo.ok(null);
        // TODO: 2021/12/4 支付功能项目三完成
        return  BaseRespVo.error(724,"订单不能支付");
    }

    //TODO 订单提交未调试
    @PostMapping("submit")
    public BaseRespVo submit(@RequestBody WxOrderBO order) {

        Integer orderId = service.orderSubmit(order);
        OrderVO orderVO = new OrderVO();
        orderVO.setGrouponLinkId(0);//写死
        orderVO.setOrderId(orderId);
        //用map返回，不知道能不能成
        return BaseRespVo.ok(orderVO);
    }





}
